package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    /**
     * 测试
     */
    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    /**
     * 检查手机号是否重复
     */
    @GetMapping("/checkPhone")
    public boolean checkPhone(String phone) {

        return !userInfoService.checkPhone(phone);
    }

    /**
     * 发送验证码
     */
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {

        userInfoService.sendVerifyCode(phone);

        return JsonResult.success();
    }

    /**
     * 注册
     */
    @ApiOperation(value = "用户注册", notes = "注册接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号", name = "phone", dataType = "string", required = true)
    })
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        try {
            userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        }catch (LogicException le) {
            le.printStackTrace();
            // 错误
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM, le.getMessage(), null);

        }catch (RuntimeException e) {
            // 有可能jedis出问题了 不能保存到
            // 爆了一些系统的 没有自定义的错误信息 的 异常
            // 不能给用户看
            // 所以需要区分一下异常
            e.printStackTrace();
            // 错误
            return JsonResult.defaultError();
        }

        return JsonResult.success();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Object login(String username, String password) {

        UserInfo userInfo = userInfoService.login(username, password);

        String token = userInfoRedisService.setToken(userInfo);

        Map<String, Object> map = new HashMap<>();
        map.put("user", userInfo);
        map.put("token", token);

        return JsonResult.success(map);
    }


    /**
     * 获取当前登录用户
     */
    @GetMapping("/currentUser")
    @RequireLogin
    public Object currentUser(HttpServletRequest request) {

        String token = request.getHeader("token");

        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        return JsonResult.success(userInfo);
    }

    /**
     * 获取某个用户的所有收藏攻略的id
     */
    @GetMapping("/strategies/favor")
    public Object favor(String userId) {

        List<String> list = strategyStatisVORedisService.getUserStrategyFavor(userId);

        return JsonResult.success(list);
    }

}





































